import { RouterType } from "../types";
import { createRouter, createWebHistory } from 'vue-router'
//1级路由
import Default from "../view/Homes.vue"
import Login from "../view/Login.vue"
import Sign from "../view/Sign.vue"
import UserInfos from "../view/child/UserInfos.vue"
//2级及后代路由
import Worktable from "../view/child/Worktable.vue"

import TextAll from "../view/child/TextAll.vue"
import setTag from "../view/child/setTag.vue"
import setList from "../view/child/SetList.vue"

import Pages from "../view/child/Page.vue"
import Knowledge from "../view/child/Knowledge.vue"
import Poster from "../view/child/Poster.vue"
import Comment from "../view/child/Comment.vue"
import Emiles from "../view/child/Emile.vue"
import Files from "../view/child/Files.vue"
import Searchs from "../view/child/Searchs.vue"
import Visit from "../view/child/Visit.vue"
import Users from "../view/child/User.vue"
import Edits from "../view/child/Edit.vue"
// import text from "../view/child/text.vue"
import artedit from "../view/child/artedit.vue"
import addtext from "../view/child/addtext.vue"
import addpage from "../view/child/addpage.vue"
import editpage from "../view/child/editpage.vue"
export const childrens: RouterType[] = [
    {
        path: "/",
        component: Worktable,
        meta: {
            title: "工作台"
        }
    },
    {
        path: "/textall",
        component: TextAll,
        meta: {
            title: "所有文章"
        }
    },
    {
        path: "/setList",
        component: setList,
        meta: {
            title: "分类设置"
        }
    }, {
        path: "/settag",
        component: setTag,
        meta: {
            title: "标签管理"
        }
    }
    ,
    {
        path: "/page",
        component: Pages,
        meta: {
            title: "页面管理"
        }
    },
    {
        path: "/knowledge",
        component: Knowledge,
        meta: {
            title: "知识小测"
        }
    }, {
        path: "/poster",
        component: Poster,
        meta: {
            title: "海报管理"
        }
    }, {
        path: "/comment",
        component: Comment,
        meta: {
            title: "评论管理"
        }
    }, {
        path: "/emiles",
        component: Emiles,
        meta: {
            title: "邮件管理"
        }
    }, {
        path: "/files",
        component: Files,
        meta: {
            title: "文件管理"
        }
    }, {
        path: "/searchs",
        component: Searchs,
        meta: {
            title: "搜索记录"
        }
    }, {
        path: "/visit",
        component: Visit,
        meta: {
            title: "访问统计"
        }
    }, {
        path: "/users",
        component: Users,
        meta: {
            title: "用户管理"
        }
    }, {
        path: "/edit",
        component: Edits,
        meta: {
            title: "系统设置"
        }
    }
    , {
        path: "/userinfos",
        component: UserInfos,
        meta: {
            title: "个人中心"
        }
    } , {
        path: "/artedit/:id",
        component: artedit,
        meta: {
            title: "文章编辑器"
        }
    }, {
        path: "/addtext",
        component: addtext,
        meta: {
            title: "添加文章"
        }
    }, {
        path: "/addpage",
        component: addpage,
        meta: {
            title: "添加页面"
        }
    }, {
        path: "/editpage/:id",
        component: editpage,
        meta: {
            title: "页面编辑"
        }
    }
]

export const childrenes: RouterType[] = [
    {
        path: "/",
        component: Worktable,
        meta: {
            title: "工作台"
        }
    },
    {
        path:"/setlist",
        component:setList,
        meta:{
            title:"文章管理"
        },
        children:[
            {
                path: "/textall",
                component: TextAll,
                meta: {
                    title: "所有文章"
                }
            },
            {
                path: "/setList",
                component: setList,
                meta: {
                    title: "分类设置"
                }
            }, {
                path: "/settag",
                component: setTag,
                meta: {
                    title: "标签管理"
                }
            }
            ,
        ]
    },
    {
        path: "/page",
        component: Pages,
        meta: {
            title: "页面管理"
        }
    },
    {
        path: "/knowledge",
        component: Knowledge,
        meta: {
            title: "知识小测"
        }
    }, {
        path: "/poster",
        component: Poster,
        meta: {
            title: "海报管理"
        }
    }, {
        path: "/comment",
        component: Comment,
        meta: {
            title: "评论管理"
        }
    }, {
        path: "/emiles",
        component: Emiles,
        meta: {
            title: "邮件管理"
        }
    }, {
        path: "/files",
        component: Files,
        meta: {
            title: "文件管理"
        }
    }, {
        path: "/searchs",
        component: Searchs,
        meta: {
            title: "搜索记录"
        }
    }, {
        path: "/visit",
        component: Visit,
        meta: {
            title: "访问统计"
        }
    }, {
        path: "/users",
        component: Users,
        meta: {
            title: "用户管理"
        }
    }, {
        path: "/edit",
        component: Edits,
        meta: {
            title: "系统设置"
        }
    }
    // , {
    //     path: "/userinfos",
    //     component: UserInfos,
    //     meta: {
    //         title: "个人中心"
    //     }
    // }
]

//1级路由
const MY_ROUTER_LIST: RouterType[] = [
    {
        path: "/",
        component: Default,
        meta: {
            title: "default"
        },
        children: childrens
    }, {
        path: "/login",
        component: Login,
        meta: {
            title: "login"
        }
    }, {
        path: "/sign",
        component: Sign,
        meta: {
            title: "sign"
        }
    }
]
//创建路由
const router = createRouter({
    history: createWebHistory(),
    routes: MY_ROUTER_LIST
})
//路由守卫
router.beforeEach((to, from, next) => {
    let token = localStorage.getItem("token")

    // 如果 未登录 且进入的不是登录页面 则跳转到登录页面
    let whiteList = ["/login", "/sign"];
    if (!whiteList.includes(to.path)) {
        //判断是否登录 
        if (!token) {
            next("/login")
        }
    }
    next()
})

export default router
